//
//  UIScrollView+TYZPage.m
//  AVfoundationDemo
//
//  Created by TangBin on 2019/8/14.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "UIScrollView+TYZPage.h"
#import <objc/runtime.h>

@implementation UIScrollView (TYZPage)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleInstanceMethod:NSSelectorFromString(@"_notifyDidScroll") withMethod:@selector(tyz_scrollViewDidScrollView)];
        [self swizzleInstanceMethod:NSSelectorFromString(@"_scrollViewWillBeginDragging") withMethod:@selector(tyz_scrollViewWillBeginDragging)];
    });
}

- (void)tyz_scrollViewDidScrollView
{
    [self tyz_scrollViewDidScrollView];
    if (self.tyz_observerDidScrollView && self.pageScrollViewDidScrollView)
    {
        self.pageScrollViewDidScrollView(self);
    }
}

- (void)tyz_scrollViewWillBeginDragging
{
    [self tyz_scrollViewWillBeginDragging];
    if (self.tyz_observerDidScrollView && self.pageScrollViewBeginDragginScrollView)
    {
        self.pageScrollViewBeginDragginScrollView(self);
    }
}

#pragma mark - Getter - Setter

- (BOOL)tyz_observerDidScrollView
{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setTyz_observerDidScrollView:(BOOL)tyz_observerDidScrollView
{
    objc_setAssociatedObject(self, @selector(tyz_observerDidScrollView), @(tyz_observerDidScrollView), OBJC_ASSOCIATION_ASSIGN);
}

- (TYZPageScrollViewDidScrollView)pageScrollViewDidScrollView
{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setPageScrollViewDidScrollView:(TYZPageScrollViewDidScrollView)pageScrollViewDidScrollView
{
    objc_setAssociatedObject(self, @selector(pageScrollViewDidScrollView), pageScrollViewDidScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (TYZPageScrollViewBeginDragginScrollView)pageScrollViewBeginDragginScrollView
{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setPageScrollViewBeginDragginScrollView:(TYZPageScrollViewBeginDragginScrollView)pageScrollViewBeginDragginScrollView
{
    objc_setAssociatedObject(self, @selector(pageScrollViewBeginDragginScrollView), pageScrollViewBeginDragginScrollView, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

#pragma amrk - Swizzle
+ (void)swizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
    Class cls = [self class];
    Method originalMethod = class_getInstanceMethod(cls, origSelector);
    Method swizzledMethod = class_getInstanceMethod(cls, newSelector);
    if (class_addMethod(cls,
                        origSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod)) ) {
        class_replaceMethod(cls,
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
        
    } else {
        class_replaceMethod(cls,
                            newSelector,
                            class_replaceMethod(cls,
                                                origSelector,
                                                method_getImplementation(swizzledMethod),
                                                method_getTypeEncoding(swizzledMethod)),
                            method_getTypeEncoding(originalMethod));
    }
}

- (void)tyz_setContentOffsetY:(CGFloat)offsetY
{
    if (self.contentOffset.y != offsetY)
    {
        self.contentOffset = CGPointMake(0, offsetY);
    }
}

@end
